{	Problem: http://codeforces.ru/problemset/problem/56/B
	Verdict: Accepted

}
var i,k,l,r,prev,n:integer; flag:boolean;
begin
readln(n);
flag:=true;
l:=0;
r:=0;
prev:=0;
for i:=1 to n do begin
if flag=true then begin
  read(k);
  if k<>i then begin
    if l=0 then begin
      r:=k;
      l:=i;
    end else begin
      if prev-k<>1 then flag:=false;
      if k<L then flag:=false;
      if (k>r) and (i<=r) then flag:=false;
    end;
  end;
  prev:=k;
end;
end;
if flag=true then writeln(l,' ',r) else writeln('0 0');
readln;
end.